<script setup>
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()
const pathId = route.query.id

const emit = defineEmits(['setPassword'])
const passwordForm = ref({
  password: '',
  confirmPassword: '',
})
const passwordRef = ref(null)

const validatePassword = (rule, value, callback) => {
  if (value.length < 6) {
    callback(new Error('密码不能少于6位'))
  } else {
    callback()
  }
}

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== passwordForm.value.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const passwordRules = ref({
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      validator: validatePassword,
      trigger: 'blur',
    },
  ],
  confirmPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      validator: validateConfirmPassword,
      trigger: 'blur',
    },
  ],
})

const handleSetPassword = () => {
  passwordRef.value.validate(async (valid) => {
    if (valid) {
      emit('setPassword', { pathId, password: passwordForm.value.password })
      router.push(`/login`)
    }
  })
}
</script>

<template>
  <div class="set-password">
    <div class="header">
      <img class="logo" src="/public/images/logo.png" alt="" />
      <h1 class="title">设置密码</h1>
    </div>
    <div class="content">
      <el-form :model="passwordForm" :rules="passwordRules" ref="passwordRef" class="form-password">
        <el-form-item class="form-item" prop="password">
          <label>输入密码</label>
          <el-input
            v-model="passwordForm.password"
            type="password"
            placeholder="请输入密码"
            class="custom-input"
            :show-password="true"
          />
        </el-form-item>
        <el-form-item class="form-item" prop="confirmPassword">
          <label>确认密码</label>
          <el-input
            v-model="passwordForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            class="custom-input"
            :show-password="true"
          />
        </el-form-item>
        <el-button type="primary" class="submit-btn" @click="handleSetPassword()">
          确 定
        </el-button>
      </el-form>
    </div>
    <div class="footer">
      <div class="footer-content">
        <div class="footer-item">
          <router-link to="/login">已有账号，去登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.set-password {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .header {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .logo {
      width: 100px;
    }

    .title {
      position: absolute;
      left: 50%;
      font-size: 24px;
      font-weight: bold;
      transform: translate(-50%);
    }
  }

  .content {
    width: 100%;
    padding: 10px;

    .form-password {
      .form-item {
        margin-bottom: 24px;

        label {
          display: block;
          margin-bottom: 8px;
          color: #000000;
          font-size: 16px;
        }

        .custom-input {
          :deep(.el-input__wrapper) {
            padding: 10px;
            font-size: medium;
          }
        }
      }
      .submit-btn {
        width: 100%;
        height: 40px;
        margin-top: 10px;
        border-radius: 6px;
        font-size: medium;
      }
    }
  }

  .footer {
    .footer-content {
      .footer-item {
        margin-top: 10px;
        text-align: center;
        a {
          color: #409eff;
          font-size: 14px;

          &:hover {
            text-decoration: underline;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>
